//**************************************************************************
// Copyright 2008 Peter Project (http://www.codeplex.com/peter)
//
// License : GNU General Public License (GPL)
// For full text of the license please goto:
// http://www.gnu.org/licenses/gpl-2.0.html
//
// This code is provided on an AS IS basis, with no WARRANTIES,
// CONDITIONS or GUARANTEES of any kind.
//
//		File: PeterInterfaces.cs
//		Purpose: This file holds all the Interfaces for the Peter
//				 Plugin Architecture.
//
//**************************************************************************

using System;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using PeterInterface;
using WeifenLuo.WinFormsUI.Docking;

namespace Peter
{
    public partial class ctrlCodeStructure : DockContent, IPeterPluginTab
    {
        private IPeterPluginHost m_Host;
        private bool m_CanScroll;
        private TextEditorControl m_Editor;

        public ctrlCodeStructure()
        {
            InitializeComponent();
            this.m_CanScroll = true;
            this.TabText = "Code Structure";
            this.treeMain.AfterSelect += new TreeViewEventHandler(treeMain_AfterSelect);
        }

        #region -= Helpers =-

        /// <summary>
        /// Gets or Sets if we are able to scroll to the selected item...
        /// </summary>
        public bool CanScroll
        {
            get { return this.m_CanScroll; }

            set { this.m_CanScroll = value; }
        }

        /// <summary>
        /// After an item is selected, scroll to it...
        /// </summary>
        /// <param name="sender">TreeView</param>
        /// <param name="e">TreeViewEventArgs</param>
        void treeMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.tsbGotoCode.Checked)
            {
                if (this.m_CanScroll)
                {
                    if (e.Node.Parent != null)
                    {
                        if (e.Node.Tag != null)
                        {
                            int off = Convert.ToInt32(e.Node.Tag.ToString());
                            int index = e.Node.Text.IndexOf(":");
                            int temp = e.Node.Text.IndexOf("(");
                            if ((temp < index && temp > 0) || index == -1)
                            {
                                index = temp;
                            }
                            temp = e.Node.Text.IndexOf(" ");
                            if ((temp < index && temp > 0) || index == -1)
                            {
                                index = temp;
                            }
                            int pos;
                            if (index < 0)
                            {
                                pos = off + e.Node.Text.Length;
                            }
                            else
                            {
                                pos = off + index;
                            }
                            this.ScrollToOffset(off, pos);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Occurs when the active content is changed...
        /// </summary>
        /// <param name="content">New Content</param>
        public void ActiveContentChanged (IDockContent content)
        {
            DockContent d = (DockContent)content;
            TextEditorControl edit = null;
            foreach (Control c in d.Controls)
            {
                if (c.GetType().ToString().Equals(typeof(TextEditorControl).ToString()))
                {
                    edit = (TextEditorControl)c;
                    break;
                }
            }
            if (edit != this.m_Editor)
            {
                this.m_Editor = edit;
                this.ParseCode();
            }
            if (this.ItemCount == 0 && this.m_Editor != null)
            {
                this.ParseCode();
            }
        }

        /// <summary>
        /// Scrolls the active editor to the given offset...
        /// </summary>
        /// <param name="offset">offset to start at</param>
        /// <param name="pos">offset to end at</param>
        public void ScrollToOffset (int offset, int pos)
        {
            int line = this.m_Editor.Document.GetLineNumberForOffset(offset);
            this.m_Editor.ActiveTextAreaControl.Caret.Position = this.m_Editor.Document.OffsetToPosition(offset);
            this.m_Editor.ActiveTextAreaControl.SelectionManager.SetSelection(
                this.m_Editor.Document.OffsetToPosition(offset), this.m_Editor.Document.OffsetToPosition(pos));
            this.m_Editor.ActiveTextAreaControl.CenterViewOn(line, 0);
            this.m_Editor.ActiveTextAreaControl.TextArea.Focus();
        }

        /// <summary>
        /// Clears the list view's items and groups...
        /// </summary>
        public void Clear()
        {
            this.treeMain.Nodes.Clear();
        }

        /// <summary>
        /// Adds a node to the tree...
        /// </summary>
        /// <param name="node">Node to Add</param>
        public void AddNode(TreeNode node)
        {
            this.treeMain.Nodes.Add(node);
        }

        /// <summary>
        /// Gets the number of Items in the TreeView...
        /// </summary>
        public int ItemCount
        {
            get { return this.treeMain.Nodes.Count; }
        }

        /// <summary>
        /// Gets the Nodes for the tree...
        /// </summary>
        /// <returns>TreeNodeCollection</returns>
        public TreeNodeCollection TreeNodes()
        {
            return this.treeMain.Nodes;
        }

        /// <summary>
        /// Sets the image list of the tree...
        /// </summary>
        /// <param name="fileExt">Image list name to set...</param>
        public void SetImageList(string fileExt)
        {
            switch (fileExt)
            {
                case ".css":
                    this.treeMain.ImageList = imgCSS;
                    break;
                default:
                    this.treeMain.ImageList = null;
                    break;
            }
        }

        #endregion

        #region IPeterPluginTab Members

        public void Save()
        {
        }

        public void SaveAs(string filePath)
        {
        }

        public void Cut()
        {
        }

        public void Copy()
        {
        }

        public void Paste()
        {
        }

        public void Undo()
        {
        }

        public void Redo()
        {
        }

        public void Delete()
        {
        }

        public void Duplicate()
        {
        }

        public void Print()
        {
        }

        public void SelectAll()
        {
        }

        public bool CloseTab()
        {
            this.Close();
            return true;
        }

        public void MarkAll(System.Text.RegularExpressions.Regex reg)
        {
        }

        public bool FindNext(System.Text.RegularExpressions.Regex reg, bool searchUp)
        {
            return false;
        }

        public void ReplaceNext(System.Text.RegularExpressions.Regex reg, string replaceWith, bool searchUp)
        {
        }

        public void ReplaceAll(System.Text.RegularExpressions.Regex reg, string replaceWith)
        {
        }

        public void SelectWord(int line, int offset, int wordLeng)
        {
        }

        public IPeterPluginHost Host
        {
            get { return this.m_Host; }

            set { this.m_Host = value; }
        }

        public string FileName
        {
            get { return ""; }
        }

        public string Selection
        {
            get { return ""; }
        }

        public bool AbleToUndo
        {
            get { return false; }
        }

        public bool AbleToRedo
        {
            get { return false; }
        }

        public bool NeedsSaving
        {
            get { return false; }
        }

        public bool AbleToPaste
        {
            get { return false; }
        }

        public bool AbleToCut
        {
            get { return false; }
        }

        public bool AbleToCopy
        {
            get { return false; }
        }

        public bool AbleToSelectAll
        {
            get { return false; }
        }

        public bool AbleToSave
        {
            get { return false; }
        }

        public bool AbleToDelete
        {
            get { return false; }
        }

        #endregion

        #region -= Tool Bar =-

        private void tsbExpandAll_Click(object sender, EventArgs e)
        {
            for (int a = 0; a < this.treeMain.Nodes.Count; a++)
            {
                this.treeMain.Nodes[a].ExpandAll();
            }
        }

        private void tsbCollapseAll_Click(object sender, EventArgs e)
        {
            for (int a = 0; a < this.treeMain.Nodes.Count; a++)
            {
                this.treeMain.Nodes[a].Collapse();
            }
        }

        #endregion

        private void ParseCode ()
        {
            this.Clear();

            if (this.m_Editor != null)
            {
                if (!string.IsNullOrEmpty(this.m_Editor.FileName))
                {
                    string ext = Path.GetExtension(this.m_Editor.FileName).ToLower();
                    this.SetImageList(ext);
                    this.m_CanScroll = true;

                    switch (ext)
                    {
                        /*.manifest;.config;.addin;.wxs;.wxi;.wxl;.proj;
                         * .ilproj;.booproj;.build;.xfrm;.targets;
                         * .xpt;.xft;.map;.wsdl;.disco*/
                        case ".xml":
                        case ".xshd":
                        case ".csproj":
                        case ".vbproj":
                        case ".xaml":
                        case ".xsl":
                        case ".xslt":
                        case ".xsd":
                        case ".jpx":
                            this.m_CanScroll = false;
                            XmlParser.ParseToTree(this.m_Editor.FileName, this.TreeNodes());
                            break;
                        case ".cs":
                            CSParser.CSParser.ParseToTree(this.m_Editor.FileName, this.TreeNodes());
                            break;
                        case ".css":
                            this.m_CanScroll = false;
                            CSSParser.CSSParser.ParseToTree(this.m_Editor.FileName, this.TreeNodes());
                            break;
                        case ".java":
                            JavaParser.JavaParser.ParseToTree(this.m_Editor.FileName, this.TreeNodes());
                            break;
                        case ".c":
                        case ".h":
                            CParser.CParser.ParseToTree(this.m_Editor.FileName, this.TreeNodes());
                            break;
                    }
                }
            }
        }
    }
}
